package com.brilliance.project.webapp.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.brilliance.project.common.Constants;
import com.brilliance.project.webapp.form.sysmanage.AgentForm;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 记录log信息
	public Log log = LogFactory.getLog(this.getClass());
	// 跳转到json
	protected static final String JSON = "toJson";
	// 是否关闭窗口
	// 当=true时关闭pop窗口
	private boolean ifCloseWindow = false;
	/**
	 * 标志画面是否显示message信息
	 */
	private boolean ifShowMsg = false;
	/**
	 * 画面需要pop出的提示讯息
	 */
	private String popMsg;
	// ====用于easyUi dataGrid start===================================
	public List<Object> rows = new ArrayList<Object>();
	public int total = 0;
	// 每页记录数
	private int pageSize = Constants.DEFAULT_MAX_PAGE_RECORD;
	// =====用于easyUi dataGrid end==================================

	// struts自动注入request，session，application
	protected Map<String, Object> request;
	protected Map<String, Object> session;
	protected Map<String, Object> application;

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	/**
	 * 返回HttpServletResponse
	 * 
	 * @return HttpServletResponse
	 */
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 返回HttpServletRequest
	 * 
	 * @return
	 */
	public HttpServletRequest getHttpServletRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 返回上下文环境
	 * 
	 * @return ServletContext
	 */
	public ServletContext getContext() {
		return getHttpServletRequest().getSession().getServletContext();
	}

	/**
	 * 返回sessionId
	 * 
	 * @return
	 */
	public String getSessionId() {
		return getHttpServletRequest().getSession().getId();
	}

	/**
	 * 依据userId在application中查询此用户的sessionId
	 * 
	 * @param userId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getUserSessionId(String userId) {
		Map<String, String> map = (Map<String, String>) application
				.get(Constants.SESSION_AGENT_KEY);
		Object obj = map.get(userId);
		if (obj != null) {
			return (String) map.get(userId);
		} else {
			return "";
		}
	}

	public void removeSessionId() {

	}

	// 从session中获得agent
	public AgentForm getAgent() {
		Object obj = getHttpServletRequest().getSession().getAttribute(
				Constants.SESSION_AGENT_KEY);
		if (null != obj) {
			return (AgentForm) obj;
		} else {
			return null;
		}
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public boolean isIfShowMsg() {
		return ifShowMsg;
	}

	public void setIfShowMsg(boolean ifShowMsg) {
		this.ifShowMsg = ifShowMsg;
	}

	public String getPopMsg() {
		return popMsg;
	}

	public void setPopMsg(String popMsg) {
		this.popMsg = popMsg;
	}

	public boolean isIfCloseWindow() {
		return ifCloseWindow;
	}

	public void setIfCloseWindow(boolean ifCloseWindow) {
		this.ifCloseWindow = ifCloseWindow;
	}

	public List<Object> getRows() {
		return rows;
	}

	public void setRows(List<Object> rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Log getLog() {
		return log;
	}

	public void setLog(Log log) {
		this.log = log;
	}

}
